package com.herui.iot.ui.dialog;

import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.herui.device_lib.listener.DeviceLis;
import com.herui.device_lib.listener.DeviceLisAdapter;
import com.herui.device_lib.presenter.DevicePresenterInter;
import com.herui.sdyu_lib.adapter.recycle_view.base.ViewHolder;
import com.herui.sdyu_lib.base.BaseActivity;
import com.herui.sdyu_lib.utils.ToastUtil;
import com.herui.sport_care.R;
import com.herui.sport_care.ui.BaseListView;
import com.herui.sport_care.utils.ViewTools;
import com.protontek.healthy.bluetoothcore.ble.scanner.bean.ScanResult;
import com.protontek.healthy.sdkcore.sphygmomanometer.api.BPDataBean;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by amanda.shan on 2022/12/6.
 */
public class BpManagerDialog {

    @BindView(R.id.view_scan)
    View view_scan;
    @BindView(R.id.id_recyclerview)
    RecyclerView id_recyclerview;
    @BindView(R.id.view_detect)
    View view_detect;
    @BindView(R.id.tv_mac)
    TextView tv_mac;
    @BindView(R.id.view_bp_result)
    View view_bp_result;
    @BindView(R.id.tv_sbp)
    TextView tv_sbp;
    @BindView(R.id.tv_dbp)
    TextView tv_dbp;
    @BindView(R.id.btn_check)
    Button btn_check;

    private Dialog dialog;
    private DialogLis dialogLis;

    private DevicePresenterInter devicePresenter;
    BaseListView baseListView;

    private String connectMac;
    private int sbp, dbp;
    private boolean finishFlag = false;

    /**
     * @param context
     */
    public BpManagerDialog(BaseActivity context, DevicePresenterInter devicePresenter,
                           DialogLis dialogLis) {

        this.dialogLis = dialogLis;
        this.devicePresenter = devicePresenter;

        View layout = context
                .getLayoutInflater().inflate(R.layout.dialog_bp,
                        null);
        ButterKnife.bind(this, layout);
        dialog = new Dialog(context, R.style.MyDialogStyleBottom);
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setOnDismissListener(dialog1 -> {
            DeviceLisAdapter.remove(view);
        });
        dialog.show();
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setLayout((int) ViewTools.fromDpToPx(350), WindowManager
                .LayoutParams.WRAP_CONTENT);
        dialogWindow.setGravity(Gravity.CENTER);

        initUi(context);

        DeviceLisAdapter.add(view);
        devicePresenter.scanBp();

    }

    private void initUi(BaseActivity context) {
        view_scan.setVisibility(View.VISIBLE);
        view_detect.setVisibility(View.INVISIBLE);
        baseListView = new BaseListView<ScanResult>(context) {
            @Override
            protected void convert(ViewHolder holder, ScanResult value, int position) {
                holder.setText(R.id.tv_mac, value.getDevice().getAddress());
//                holder.setText(R.id.tv_mac, "123456789");
                holder.setOnClickListener(R.id.btn_connect, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (value != null) {
                            devicePresenter.connectBp(value.getDevice().getAddress());
                            connectMac = value.getDevice().getAddress();
                        } else {
                            devicePresenter.connectBp("123456789");
                            connectMac = "123456789";
                        }
                    }
                });
            }

            @Override
            public void doRefresh() {

            }

            @Override
            public void addData(ScanResult scanResult) {
                List<ScanResult> scanResults = getmDatas();
                boolean flag = false;
                for (int i = 0; i < scanResults.size(); i++) {
                    if (scanResults.get(i).getDevice().getAddress().equals(scanResult.getDevice().getAddress())) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    super.addData(scanResult);
                }
            }
        };
        baseListView.initList(id_recyclerview, R.layout.item_bp_list, false, null);
    }


    @OnClick({R.id.btn_check})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.btn_check:
                if (finishFlag){
                    dismiss();
                }else{
                    // 开始检测血压
                    devicePresenter.startBp();
                }
                break;
            default:
                break;
        }
    }

    // 连接成功
    private void connectOk() {
        view_scan.setVisibility(View.GONE);
        view_detect.setVisibility(View.VISIBLE);
        tv_mac.setText(connectMac);
    }

    DeviceLis view = new DeviceLis() {
        @Override
        public void onBpDevice(ScanResult scanResult) {
            view_scan.post(() -> {
                baseListView.addData(scanResult);
            });
        }

        @Override
        public void onConnectBp(boolean flag) {
            view_scan.post(() -> {
                if (flag) {
                    connectOk();
                } else {
                    ToastUtil.showMessage("连接失败");
                }
            });
        }

        @Override
        public void onStartBp() {
            view_scan.post(() -> {
                view_scan.setVisibility(View.GONE);
                view_detect.setVisibility(View.VISIBLE);
                view_bp_result.setVisibility(View.VISIBLE);
                tv_sbp.setText("....");
                tv_dbp.setText("....");
                btn_check.setText("正在测量......");
            });
        }

        @Override
        public void onBpResult(BPDataBean bpDataBean) {
            view_scan.post(() -> {
                tv_sbp.setText(bpDataBean.getSystolicBp() + "");
                tv_dbp.setText(bpDataBean.getDiastolicBp() + "");
                btn_check.setText("结束测量");
                sbp = (int) bpDataBean.getSystolicBp();
                dbp = (int) bpDataBean.getDiastolicBp();
                dialogLis.onOk(bpDataBean);
                finishFlag = true;
            });
        }
    };

    public void setDialogLis(DialogLis dialogLis) {
        this.dialogLis = dialogLis;
    }

    public void dismiss() {
        dialog.dismiss();
    }

    public void show() {
        dialog.show();
    }

    public interface DialogLis {
        void onOk(BPDataBean bpDataBean);
    }
}

